﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace DAL
{
    public class AdminDAL : IAdminDAL
    {

        private static readonly string SQL_CONN_SQLSERVER = ConfigurationManager.AppSettings["DBConn"];


        /// <summary>
        /// 根据传入的ID判断一条数据是否存在
        /// </summary>       
        public bool idExists(int adminId)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT AD_adminId FROM Admin WHERE AD_adminId = @adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminId", adminId);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }


        /// <summary>
        /// 根据传入的adminname判断一条数据是否存在
        /// </summary>       
        public bool nameExists(string adminName)
        {
            //Set up a return value
            bool isExist = false;
            string sqlStatement = "SELECT AD_adminName FROM Admin WHERE AD_adminName = @adminName";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminName", adminName);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        isExist = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isExist;

        }



        /// <summary>
        /// 增加一条数据
        /// </summary>                
        public bool Add(string adminName, string adminPsd, System.DateTime lastLoginTime, string lastLoginIP)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "INSERT INTO UserLgoin VALUES(@adminId, @adminName, @adminPsd,@lastLoginTime,@lastLoginIP )";


            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@adminName", adminName), new SqlParameter("@adminPsd", adminPsd), new SqlParameter("@lastLoginTime", lastLoginTime), new SqlParameter("@lastLoginIP", lastLoginIP) };

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>                
        public bool Add(Model.Admin admin)
        //Set up a return value
        {
            bool opetation = false;
            string sqlStatement = "INSERT INTO UserLgoin VALUES(@adminName, @adminPsd,@lastLoginTime,@lastLoginIP )";


            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@adminName", admin.getAD_adminName()), new SqlParameter("@adminPsd", admin.getAD_adminPsd()), new SqlParameter("@lastLoginTime", admin.getAD_lastLoginTime()), new SqlParameter("@lastLoginIP", admin.getAD_lastLoginIP()) };

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>   
        ///  //private int AD_adminId;
        //private string AD_adminName;
        //private string AD_adminPsd;
        // private DateTime AD_lastLoginTime;
        //private string AD_lastLoginIP;
        public bool Update(int adminId, string adminName, string adminPsd, System.DateTime lastLoginTime, string lastLoginIP)
        {     //Set up a return value
            bool opetation = false;
            string sqlStatement = "UPDATE Admin SET AD_adminName=@adminName,AD_adminPsd=@adminPsd,AD_lastLoginTime=@lastLoginTime,AD_lastLoginIP=@lastLoginIP WHERE AD_adminId=@adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@adminName", adminName), new SqlParameter("@adminPsd", adminPsd), new SqlParameter("@lastLoginTime", lastLoginTime), new SqlParameter("@lastLoginIP", lastLoginIP),  new SqlParameter("@adminId", adminId)};

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>             
        public bool Update(Model.Admin admin)
        {     //Set up a return value
            bool opetation = false;
            string sqlStatement = "UPDATE Admin SET AD_adminName=@adminName,AD_adminPsd=@adminPsd,AD_lastLoginTime=@lastLoginTime,AD_lastLoginIP=@lastLoginIP WHERE AD_adminId=@adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter[] paras;
                paras = new SqlParameter[] { new SqlParameter("@adminName", admin.getAD_adminName()), new SqlParameter("@adminPsd", admin.getAD_adminPsd()), new SqlParameter("@lastLoginTime", admin.getAD_lastLoginTime()), new SqlParameter("@lastLoginIP", admin.getAD_lastLoginIP()), new SqlParameter("@adminId", admin.getAD_adminId())};

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, paras);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }



        /// <summary>
        /// 根据传入的adminId删除一条数据
        /// </summary>
        public bool adminIdDelete(int adminId)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "DELETE FROM Admin WHERE AD_adminId = @adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminId", adminId);

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, para);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }



        /// <summary>
        /// 根据传入的adminName删除一条数据
        /// </summary>
        public bool adminNameDelete(string adminName)
        {
            //Set up a return value
            bool opetation = false;
            string sqlStatement = "DELETE FROM Admin WHERE AD_adminName = @adminName";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminName", adminName);

                //Execute the delete	
                int lineNum = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlStatement, para);
                {
                    if (lineNum > 0)
                        opetation = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return opetation;
        }


        /// <summary>
        /// 根据传入的adminId得到adminName
        /// </summary>       
        public string getAdminName(int adminId)
        {
            //Set up a return value
            string adminName = string.Empty;

            string sqlStatement = "SELECT AD_adminName FROM Admin WHERE AD_adminId = @adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminId", adminId);

                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        adminName = Convert.ToString(rdr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return adminName;
        }


        /// <summary>
        /// 根据传入的adminName得到密码
        /// </summary>
        public string getPsdByName(string adminName)
        {
            //Set up a return value
            string password = null;
            string sqlStatement = "SELECT AD_adminPsd FROM Admin WHERE AD_adminName = @adminName";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminName", adminName);

                //Execute the delete	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        password = Convert.ToString(rdr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return password;
        }



        /// <summary>
        /// 根据传入的adminId得到密码
        /// </summary>
        public string getPsdById(int adminId)
        {
            //Set up a return value
            string password = null;
            string sqlStatement = "SELECT AD_adminPsd FROM Admin WHERE AD_adminId = @adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminId", adminId);

                //Execute the delete	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    if (rdr.Read())
                        password = Convert.ToString(rdr.GetString(0));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return password;
        }



        /// <summary>
        /// 根据adminId得到一个对象实体
        /// </summary>

        public Model.Admin GetModel(int adminId)
        {
            //Set up a return value
            Model.Admin admin = new Model.Admin();

            string sqlStatement = "SELECT  AD_adminId,AD_adminName,AD_adminPsd,AD_lastLoginTime,AD_lastLoginIP FROM Admin WHERE AD_adminId = @adminId";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminId", adminId);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    while (rdr.Read())
                    {
                        admin.setAD_adminId(Convert.ToInt32(rdr.GetInt32(0)));
                        admin.setAD_adminName(Convert.ToString(rdr.GetString(1)));
                        admin.setAD_adminPsd(Convert.ToString(rdr.GetString(2)));
                        admin.setAD_lastLoginTime(Convert.ToDateTime(rdr.GetDateTime(3)));
                        admin.setAD_lastLoginIP(Convert.ToString(rdr.GetString(4)));
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return admin;

        }


        /// <summary>
        /// 根据adminName得到一个对象实体
        /// </summary>
        public Model.Admin GetModelByName(string adminName) 
        {
            //Set up a return value
            Model.Admin admin = new Model.Admin();

            string sqlStatement = "SELECT  AD_adminId,AD_adminName,AD_adminPsd,AD_lastLoginTime,AD_lastLoginIP FROM Admin WHERE AD_adminName = @adminName";

            try
            {
                SqlConnection conn = new SqlConnection(SQL_CONN_SQLSERVER);

                SqlParameter para;
                para = new SqlParameter("@adminName", adminName);

                //Execute the query	
                using (SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, sqlStatement, para))
                {
                    while (rdr.Read())
                    {
                        admin.setAD_adminId(Convert.ToInt32(rdr.GetInt32(0)));
                        admin.setAD_adminName(Convert.ToString(rdr.GetString(1)));
                        admin.setAD_adminPsd(Convert.ToString(rdr.GetString(2)));
                        admin.setAD_lastLoginTime(Convert.ToDateTime(rdr.GetDateTime(3)));
                        admin.setAD_lastLoginIP(Convert.ToString(rdr.GetString(4)));
                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return admin;
        }

    }
}
